/***************************消息对象***********************/
//单个消息对象
let MessageNode = function () {
    this.messageNode = document.createElement("div");
    this.messageNode.className = "message-item";
};
// 消息节点不存在
MessageNode.prototype.setNullNode = function (range) {
    if(range !==3){
        this.messageNode.innerHTML = '暂无通知';
    }else {
        this.messageNode.innerHTML = '暂无';
    }
    this.messageNode.style = 'padding: 0 0 0 15px;'
    return this.messageNode;
};
// 消息节点存在
MessageNode.prototype.setMessageNode = function (data) {
    if (data.attachType === "notice") {
        this.messageNode.innerHTML =
            '<div class="message-logo">' +
            '<img class="message-logo-img" src="http://photos-share01.oss-cn-shenzhen.aliyuncs.com/photos01/1505309326866.jpg">' +
            '</div>' +
            '<div class="message-text">' +
            '<div class="text-header"><span class="">通知</span> by <span class="">系统</span></div>' +
            '<div class="text-middle">' + data.sysContent + '</div>' +
            '</div>' +
            '<div class="message-close"><i class="fa fa-close"></i></div>'
    } else if (data.attachType === "share") {
        if (data.interact === "praise")
            this.messageNode.innerHTML =
                '<div class="message-logo">' +
                '<img class="message-logo-img" src="http://photos-share01.oss-cn-shenzhen.aliyuncs.com/photos01/1505309326866.jpg">' +
                '</div>' +
                '<div class="message-text">' +
                '<div class="text-header"><span class="">' + '<span class="message-sharePrise-informer">' + data.informer.userName + '</span>' + '  ' + '<span class="message-sharePrise-prise"> 赞 </span> 你的分享' + '</span></div>' +
                '<div class="text-middle">' + '<span class="message-sharePrise-content">' + data.attachVO.description + '</span>' + '<span class="message-sharePrise-href"><a href="' + getRootPath_web() + '/share/oneShare?shareId=' + data.attachVO.shareId + '&userId=' + data.attachVO.author.userId + '">点击跳转</a></span>' + '</div>' +
                '</div>' +
                '<div class="message-close"><i class="fa fa-close"></i></div>';
        else if (data.interact === "reply") {
            this.messageNode.innerHTML =
                '<div class="message-logo">' +
                '<img class="message-logo-img" src="http://photos-share01.oss-cn-shenzhen.aliyuncs.com/photos01/1505309326866.jpg">' +
                '</div>' +
                '<div class="message-text">' +
                '<div class="text-header"><span class="">' + '<span class="message-sharePrise-informer">' + data.informer.userName + '</span>' + '  ' + '<span class="message-sharePrise-prise"> 回复 </span> 你的评论' + '</span></div>' +
                '<div class="text-middle">' + '<span class="message-sharePrise-content">' + data.attachVO.description + '</span>' + '<span class="message-sharePrise-href"><a href="' + getRootPath_web() + '/share/oneShare?shareId=' + data.attachVO.shareId + '&userId=' + data.attachVO.author.userId + '">点击跳转</a></span>' + '</div>' +
                '</div>' +
                '<div class="message-close"><i class="fa fa-close"></i></div>';
        }
    } else if (data.attachType === "comment") {
        this.messageNode.innerHTML =
            '<div class="message-logo">' +
            '<img class="message-logo-img" src="http://photos-share01.oss-cn-shenzhen.aliyuncs.com/photos01/1505309326866.jpg">' +
            '</div>' +
            '<div class="message-text">' +
            '<div class="text-header"><span class="">' + "你的评论 " + "得到 " + "回复" + '</span></div>' +
            '<div class="text-middle">' + '<span class="message-sharePrise-content">' + data.attachVO.content + '</span>' + '<span class="message-sharePrise-href"><a href="' + getRootPath_web() + '/share/oneShare?shareId=' + data.attachVO.shareId + '&userId=' + data.attachVO.owner.userId + '">点击跳转</a></span>' + '</div>' +
            '</div>' +
            '<div class="message-close"><i class="fa fa-close"></i></div>'
    }
    return this.messageNode;
};

/**************************所有消息出现************************/
let MessageList = function () {
    this.menuList = document.querySelectorAll(".message-menu");
    this.messageList = document.querySelector(".message-content-wrapperRight");
};
MessageList.prototype.ajax_getList = function (range) {
    let data = {
            pageNum: 1,
            pageSize: 5,
            orderByField: "update_time",
            userId: loginUserId,
            range: range,
            ascend: true
        },
        method = "POST",
        url = "/info/getList";
    return ajax_base(data, url, method, "Json");
};
MessageList.prototype.setMessageTotal = function (data, range) {
    //清除父节点的子节点
    for (let i = this.messageList.children.length - 1; this.messageList.children.length !== 0 && i >= 0; i--) {
        this.messageList.removeChild(this.messageList.children[i]);
    }
    if (data.length === 0) {
        let div = new MessageNode().setNullNode(range);
        this.messageList.appendChild(div);
        return;
    }
    for (let i = 0; i < data.length; i++) {
        let div = new MessageNode().setMessageNode(data[i]);
        this.messageList.appendChild(div);
    }
};
MessageList.prototype.menuSelected = function () {
    let that = this;
    for (let i = 0; i < that.menuList.length; i++) {
        that.menuList[i].onclick = function () {
            that._clearMenu(that.menuList);
            this.className = "message-menu message-menu-selected";
            let range = parseInt(this.id);
            that.ajax_getList(range).then(function (data) {
                that.setMessageTotal(data.obj, range);
            });
        }
    }
};
MessageList.prototype._clearMenu = function (nodeList) {
    for (let i = 0; i < nodeList.length; i++) {
        nodeList[i].className = "message-menu";
    }
};
let messageList = new MessageList();
let that = messageList;
messageList.ajax_getList(0).then(function (data) {
    that.setMessageTotal(data.obj);
});
messageList.menuSelected();